package com.znjb.myapplication;

import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.support.v4.widget.DrawerLayout;
import androidx.appcompat.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.znjb.myapplication.fragment.TestFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    private DrawerLayout drawerLayout;
    private PagerSlidingTab slidingTab;
    private ViewPager viewPager;
    private TextView tvAdd;

    ActionBarDrawerToggle drawerToggle;
    MainPagerAdapter adapter;
    ArrayList list;

    private List<String> itemDatas;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        //初始化布局
        inintView();
        //初始化导航栏
        initNavigation();
        //初始化ActionBar
        initActionBar();
    }

    /**
     * 初始化导航栏
     */
    private void initNavigation() {
        //获取对应的fragment
        fragments.clear();
        for (int i = 0; i < itemDatas.size(); i++) {
            Bundle bundle = new Bundle();
            bundle.putString("title",itemDatas.get(i));
            TestFragment fragment = new TestFragment();
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }

        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(),itemDatas,fragments);
        viewPager.setAdapter(adapter);
        //将ViewPager和indicator进行绑定
        slidingTab.setViewPager(viewPager);
    }

    private void initData() {
        itemDatas = new ArrayList<>();
        itemDatas.add("首页");
        itemDatas.add("应用");
        /*itemDatas.add("游戏");
        itemDatas.add("专题");
        itemDatas.add("推荐");
        itemDatas.add("分类");
        itemDatas.add("热门");
        itemDatas.add("我的");
        itemDatas.add("最新");*/
    }

    /**
     * 初始化布局
     */
    private void inintView() {
        tvAdd = (TextView) findViewById(R.id.tvAdd);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        slidingTab = (PagerSlidingTab) findViewById(R.id.slingTab);
        viewPager = (ViewPager) findViewById(R.id.viewPager);


        tvAdd.setOnClickListener(this);
    }

    /**
     * 初始化ActionBar
     */
    private void initActionBar() {
        //获取ActionBar
        ActionBar actionBar = getSupportActionBar();
        //设置标题
        actionBar.setTitle(R.string.app_name);
        //设置图标
        actionBar.setIcon(R.mipmap.ic_launcher);

        //设置显示home键
        actionBar.setDisplayHomeAsUpEnabled(true);
        //设置home键可以被点击
        actionBar.setDisplayShowHomeEnabled(true);
        //将键头的图标变为汉堡
        drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,0,0);
        //同步ActionBar和 drawerLayout的状态
        drawerToggle.syncState();
        //给三条线增加动画
        drawerLayout.setDrawerListener(drawerToggle);

    }

    /**
     * 当点击ActionBar的home按钮时候，会执行此方法
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerToggle.onOptionsItemSelected(item);
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvAdd:
                Intent intent = new Intent(MainActivity.this,Activity_AddItem.class);
                startActivityForResult(intent,200);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200 && resultCode == 201) {
           itemDatas = data.getStringArrayListExtra("list");
            System.out.println( itemDatas.size()+"==");
            initNavigation();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
